package draft;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;

import draft.Player.Position;


/**
 * Represents a team (single person's drafted team)
 * 
 * 
 * @author doug
 */
public class Team {

	public final String name;
	private ArrayList<Player> players = new ArrayList<Player>();
//	public static ConcurrentHashMap<Position, Integer> = new ConcurrentHashMap
	
	
	public Team (String name) {
		this.name = name;
	}
	
	
	public void addPlayer (Player p) {
		players.add(p);
	}
	
	public void removePlayer (Player p) {
		players.remove(p);
	}
	
	
	double evaluate () {
		ConcurrentHashMap<Position, ArrayList<Player>> positionMap= new ConcurrentHashMap<Position, ArrayList<Player>>();
		ConcurrentHashMap<Integer, ArrayList<Player>> teamMap= new ConcurrentHashMap<Integer, ArrayList<Player>>();
		for (Player p : players){
			positionMap.putIfAbsent(p.position, new ArrayList<Player>());
			positionMap.get(p.position).add(p);
		}
		//add only starting players to score, evaluate empty positions by projecting value at that draft spot remaining
		for(Position position : Position.values()){
			ArrayList<Player> byPosition = positionMap.get(position);
			if(position == Position.TE  || position == Position.QB  || position == Position.DEF  || position == Position.K){
				if(byPosition != null){
					Player p = byPosition.get(0);
					teamMap.putIfAbsent(p.team, new ArrayList<Player>());
					teamMap.get(p.team).add(p);
				}
				else{
					Player projectedPlayer = new Player("NA", position, Minimax.getUndraftedValue(position));		
					teamMap.putIfAbsent(40, new ArrayList<Player>());
					teamMap.get(40).add(projectedPlayer);
				}
			}
			else{
				if(byPosition != null){
					Player p = byPosition.get(0);
					teamMap.putIfAbsent(p.team, new ArrayList<Player>());
					teamMap.get(p.team).add(p);
					if(byPosition.size() > 1){
						Player p2 = byPosition.get(0);
						teamMap.putIfAbsent(p2.team, new ArrayList<Player>());
						teamMap.get(p2.team).add(p2);
					}
					else{
						Player projectedPlayer = new Player("NA", position, Minimax.getUndraftedValue(position));		
						teamMap.putIfAbsent(40, new ArrayList<Player>());
						teamMap.get(40).add(projectedPlayer);
					}
				}
				else{
					Player projectedPlayer = new Player("NA", position, Minimax.getUndraftedValue(position));		
					teamMap.putIfAbsent(40, new ArrayList<Player>());
					teamMap.get(40).add(projectedPlayer);
					teamMap.get(40).add(projectedPlayer);
				}
			}
			
		}
		//normalize value based on team collision
		double teamScore = 0;
		for(ArrayList<Player> list : teamMap.values()){
			if(list.size() != 1){
				double sameTeamCount = (double) list.size();
				for(Player p : list){
					teamScore += ((double)p.rating) * (1 - (sameTeamCount-1)/10);
				}
			}
			else{
				teamScore += list.get(0).rating; 
			}
		}
		//System.out.println("Teamscore: " + teamScore + " \n" + this);
		return teamScore;
	}


	public ArrayList<Player> getPlayers() {
		return players;
	}
	
	public String toString(){
		StringBuilder s = new StringBuilder();
		s.append(name+"\n");
		for (Player p : players)
			s.append("  " + p + ", ");
		return s.toString();
	}
}
